package com.kma.summer2012.server;

import com.kma.summer2012.client.ExceptionHandling.CrumbsRPCServiceException;
import com.kma.summer2012.factory.DAOFactory;
import com.kma.summer2012.factory.SpringBeansFactory;
import com.kma.summer2012.model.CrumbsDateAndTime;
import com.kma.summer2012.model.CrumbsRequest;
import com.kma.summer2012.model.CrumbsRequestAndWork;
import com.kma.summer2012.model.CrumbsRequestsState;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.tomcat.util.http.fileupload.FileUploadException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;

/**
 * Created with IntelliJ IDEA.
 * User: Администратор
 * Date: 06.03.13
 * Time: 13:41
 * To change this template use File | Settings | File Templates.
 */
public class CrumbsFileUpload extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        DAOFactory factory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
        ServletFileUpload upload = new ServletFileUpload();
        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(CrumbsRequest.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

            FileItemIterator iter = upload.getItemIterator(request);

            FileItemStream item = iter.next();

            InputStream stream = item.openStream();

            CrumbsRequest crumbsRequest = (CrumbsRequest) jaxbUnmarshaller.unmarshal(stream);
            System.out.println(crumbsRequest.getDateOfCreation().toString());
            System.out.println(crumbsRequest.getCrumbsDateAndTimes().toString());

            CrumbsRequestsState state = factory.getCrumbsRequestsStateDAO().getById(2);
            crumbsRequest.setCrumbsRequestsState(state);
            factory.getCrumbsClientDAO().add(crumbsRequest.getCrumbsClient());

            for (CrumbsDateAndTime crumbsDateAndTime : crumbsRequest.getCrumbsDateAndTimes())
                crumbsDateAndTime.setCrumbsRequest(crumbsRequest);


            for (CrumbsRequestAndWork crumbsRequestAndWork : crumbsRequest.getCrumbsRequestAndWorks()) {
                crumbsRequestAndWork.setCrumbsRequest(crumbsRequest);
                crumbsRequestAndWork.setAmountOfWork(crumbsRequestAndWork.getAmountOfWork());
                crumbsRequestAndWork.setCrumbsCatalogOfWork(crumbsRequestAndWork.getCrumbsCatalogOfWork());
            }

            factory.getCrumbsRequestDAO().add(crumbsRequest);
            response.setStatus(HttpServletResponse.SC_CREATED);
            response.getWriter().print("Request was successfully imported ");
            response.flushBuffer();


        } catch (SQLException e) {

        } catch (JAXBException e) {
            response.setStatus(HttpServletResponse.SC_CREATED);
            response.getWriter().print("import failed");
            response.flushBuffer();


        }
        catch(Exception e){
            e.printStackTrace();

        }
    }
}
